package 二分查找;
/*
  二分查找------非递归算法
 */
public class TwofenNon {
    public static void main(String[] args) {
        int[] arr = {12,33,45,23,65,48};
        int target=65;
        int i = find(arr, arr.length,target);//数组，数组长度，目标对象
        System.out.println(i);//4

    }

    private static int find(int[] arr, int n,int target) {
        int left=0;
        int right=n-1;
        while (left<=right){
            int mid = (left+right)/2;
            if(arr[mid]==target){
                return mid;
            }else if(arr[mid]<target){
                left=mid+1;
            }else if(arr[mid]>target){
                right=mid-1;
            }
        }

//        if(left < arr.length && arr[left] == target)
//            return left;
//        if(right >= 0 && arr[right] == target)
//            return left;
//        else
        return -1;
    }



}
